package com.bugbuff.dubbo.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;

import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.bugbuff.common.pojo.PageResult;
import com.bugbuff.common.pojo.UserInfo;
import com.bugbuff.dubbo.server.api.QuanZiApi;
import com.bugbuff.dubbo.server.api.QueryInfoListApi;
import com.bugbuff.dubbo.server.api.VideoApi;
import com.bugbuff.dubbo.server.pojo.Video;
import com.bugbuff.dubbo.server.vo.MessageVo;
import com.bugbuff.dubbo.server.vo.VideoInfoVo;
import com.bugbuff.dubbo.server.vo.VideoInfoVo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class VideoInfoService {

    @Reference
    private QueryInfoListApi queryInfoListApi;
    @Autowired
    private UserInfoService userInfoService;

    @Reference
    private QuanZiApi quanZiApi;

    public PageResult queryVideoList(Integer pagesize, Integer page, String sortProp, String sortOrder, Long uid) {
        //根据uid去查询该用户的视频列表集合

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);

        List<Video> videoList = this.queryInfoListApi.queryVideoList(pagesize, page, sortProp, sortOrder, uid);

        //获取到集合之后
        if (CollUtil.isEmpty(videoList)) {
            return pageResult;
        }
        //查询用户信息
        List<Object> userIds = CollUtil.getFieldValues(videoList, "userId");

//        PageHelper.startPage(page,pagesize);
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);


        //准备封装videoinfo的集合
        List<VideoInfoVo> list = new ArrayList<>();

        for (Video video : videoList) {
            VideoInfoVo videoInfoVo = new VideoInfoVo();
//id自生成？
            videoInfoVo.setUserId(video.getUserId()); //获取userid
            videoInfoVo.setCreateDate(video.getCreated()); //创建时间
            videoInfoVo.setVideoUrl(video.getVideoUrl());//视频地址
            videoInfoVo.setPicUrl(video.getPicUrl());//图片地址
            videoInfoVo.setPublishId(video.getId().toHexString()); //视频id
            videoInfoVo.setLikeCount(Convert.toInt(this.quanZiApi.queryLikeCount(videoInfoVo.getPublishId())));//点赞总数
            videoInfoVo.setCommentCount(Convert.toInt(this.quanZiApi.queryCommentCount(videoInfoVo.getPublishId())));
            //填充用户信息
            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(videoInfoVo.getUserId(), userInfo.getUserId())) {
                    videoInfoVo.setNickname(userInfo.getNickName());
                    break;
                }
            }
            list.add(videoInfoVo);
        }

        //            对集合中的vo对象集合根据字段用比较器进行排序
        List<VideoInfoVo> sortedVoList = CollUtil.sort(list, compareList(sortProp, sortOrder));

        int[] arr = PageUtil.transToStartEnd(page - 1, pagesize);
        List<VideoInfoVo> sub = ListUtil.sub(sortedVoList, arr[0], arr[1]);

//        if (StringUtils.isNotEmpty(sortProp)) {
//            this.compareList(list, sortProp, sortOrder);
//        }else {
//            this.compareList(list, "createDate", "desc");
//        }
//
//        // 给编号
//        for (int i=1;i<=list.size();i++ ){
//            VideoInfoVo videoInfoVo = list.get(i - 1);
//            videoInfoVo.setId(""+i);
//        }
       pageResult.setCounts(this.queryInfoListApi.userVideoCount(uid));
        int pages = PageUtil.totalPage(this.queryInfoListApi.userVideoCount(uid), pagesize);
       pageResult.setPages(pages);
//        PageInfo pageInfo =new PageInfo(list);
        pageResult.setItems(sub);
        return pageResult;
    }


    public Comparator<VideoInfoVo> compareList(String sortProp, String sortOrder) {

        switch (sortProp) {
            case "createDate":
                if (StrUtil.equals("ascending", sortOrder)) {
                    return new Comparator<VideoInfoVo>() {
                        @Override
                        public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                            return o1.getCreateDate().compareTo(o2.getCreateDate());
                        }
                    };
                } else {
                    return new Comparator<VideoInfoVo>() {
                        @Override
                        public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                            return o2.getCreateDate().compareTo(o1.getCreateDate());
                        }
                    };
                }

            case "likeCount":
                if (StrUtil.equals("ascending", sortOrder)) {
                    return new Comparator<VideoInfoVo>() {
                        @Override
                        public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                            return o1.getLikeCount().compareTo(o2.getLikeCount());
                        }
                    };
                } else {
                    return new Comparator<VideoInfoVo>() {
                        @Override
                        public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                            return o2.getLikeCount().compareTo(o1.getLikeCount());
                        }
                    };
                }

            case "commentCount":
                if (StrUtil.equals("ascending", sortOrder)) {
                    return new Comparator<VideoInfoVo>() {
                        @Override
                        public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                            return o1.getCommentCount().compareTo(o2.getCommentCount());
                        }
                    };
                } else {
                    return new Comparator<VideoInfoVo>() {
                        @Override
                        public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                            return o2.getCommentCount().compareTo(o1.getCommentCount());
                        }
                    };
                }
            default:
                return new Comparator<VideoInfoVo>() {
                    @Override
                    public int compare(VideoInfoVo o1, VideoInfoVo o2) {
                        return o1.getCreateDate().compareTo(o2.getCreateDate());
                    }
                };

        }
    }
}
